//
//  CompanyPrivacySettingFilterViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/30.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "CompanyPrivacySettingFilterViewController.h"
#import "PrivacySettingCell.h"

@interface CompanyPrivacySettingFilterViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *filterTableview;
    NSMutableArray *dataArray;
    NSString *backString;
}
@end

@implementation CompanyPrivacySettingFilterViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    switch (self.type) {
        case CompanyPrivacySettingFilterTypeOfLook: {
            self.navigationItem.title = @"谁可以看我的招聘信息";
        } break;
        case CompanyPrivacySettingFilterTypeOfPush: {
            self.navigationItem.title = @"我的招聘信息被推送给谁";
        } break;
        case CompanyPrivacySettingFilterTypeOfVideo: {
            self.navigationItem.title = @"谁可以发起视频";
        } break;
        case CompanyPrivacySettingFilterTypeOfName: {
            self.navigationItem.title = @"访问职位时显示的身份";
        } break;
        case PersonalPrivacySettingFilterTypeOfLook: {
            self.navigationItem.title = @"谁可以查看我的简历";
        } break;
        case PersonalPrivacySettingFilterTypeOfPush: {
            self.navigationItem.title = @"我的简历被推送给谁";
        } break;
        case PersonalPrivacySettingFilterTypeOfName: {
            self.navigationItem.title = @"访问职位时显示的身份";
        } break;
        case PersonalPrivacySettingFilterTypeOfBlickList: {
            self.navigationItem.title = @"屏蔽企业";
        } break;
        default:
            break;
    }
    
    backString = @"";
    
    [self loadDataLocation];
    [self initUI];
}
- (void)loadDataLocation {
    dataArray = [NSMutableArray arrayWithCapacity:0];
    
    if ((self.type == CompanyPrivacySettingFilterTypeOfName) || (self.type == PersonalPrivacySettingFilterTypeOfName)) {
        PublicCategoryElementModel *firstModel = [[PublicCategoryElementModel alloc] init];
        firstModel.id = @"3";
        firstModel.name = @"真实";
        firstModel.detail = @"默认显示招聘者的真实姓+先生/女士";
        firstModel.selectType = SelectTypeOfMultiSelect;
        firstModel.isSelect = ([self.display_name integerValue]==3)?YES:NO;
        firstModel.childArray = [NSMutableArray arrayWithCapacity:0];
        [dataArray addObject:firstModel];
        
        PublicCategoryElementModel *twoModel = [[PublicCategoryElementModel alloc] init];
        twoModel.id = @"4";
        twoModel.name = @"昵称";
        twoModel.detail = @"显示填写的昵称";
        twoModel.selectType = SelectTypeOfMultiSelect;
        twoModel.isSelect = ([self.display_name integerValue]==4)?YES:NO;
        twoModel.childArray = [NSMutableArray arrayWithCapacity:0];
        [dataArray addObject:twoModel];
    } else {
        PublicCategoryElementModel *firstModel = [[PublicCategoryElementModel alloc] init];
        firstModel.id = @"1";
        firstModel.name = @"所有人";
        firstModel.detail = @"默认允许所有人";
        firstModel.selectType = SelectTypeOfMultiSelect;
        firstModel.isSelect = ([self.selectType isEqualToString:@"1"]) ? YES : NO;
        firstModel.childArray = [NSMutableArray arrayWithCapacity:0];
        [dataArray addObject:firstModel];
        
        if ([DataToolsControl sharedManager].allCategoryInfoModel && [DataToolsControl sharedManager].allCategoryInfoModel.jobAllowListArray.count) {
            PublicCategoryElementModel *twoModel = [[PublicCategoryElementModel alloc] init];
            twoModel.id = @"2";
            twoModel.name = @"部分人可查看";
            twoModel.detail = @"选择符合条件的人，可多选";
            twoModel.selectType = SelectTypeOfMultiSelect;
            twoModel.isSelect = ([self.selectType isEqualToString:@"2"]) ? YES : NO;;
            twoModel.childArray = [NSMutableArray arrayWithCapacity:0];
            if (self.type == CompanyPrivacySettingFilterTypeOfLook || self.type == PersonalPrivacySettingFilterTypeOfLook) {
                //查看
                NSArray *array = [DataToolsControl sharedManager].allCategoryInfoModel.jobAllowListArray;
                for (PublicCategoryElementModel *tempModel in array) {
                    PublicCategoryElementModel *modellll = [[PublicCategoryElementModel alloc] init];
                    modellll = [tempModel copy];
                    if (self.itemDataArray && self.itemDataArray.count) {
                        for (NSString *str in self.itemDataArray) {
                            if ([str integerValue] == [modellll.id integerValue]) {
                                modellll.isSelect = YES;
                                break;
                            }
                        }
                    }
                    [twoModel.childArray addObject:modellll];
                }
            }else if (self.type == CompanyPrivacySettingFilterTypeOfPush || self.type ==  PersonalPrivacySettingFilterTypeOfPush){
                //推送
                for (PublicCategoryElementModel *tempModel in [DataToolsControl sharedManager].allCategoryInfoModel.jobPushBanListArray) {
                    PublicCategoryElementModel *modellll = [[PublicCategoryElementModel alloc] init];
                    modellll = [tempModel copy];
                    if (self.itemDataArray && self.itemDataArray.count) {
                        for (NSString *str in self.itemDataArray) {
                            if ([str integerValue] == [modellll.id integerValue]) {
                                modellll.isSelect = YES;
                                break;
                            }
                        }
                    }
                    [twoModel.childArray addObject:modellll];
                }
            }else{
                //视频
                for (PublicCategoryElementModel *tempModel in [DataToolsControl sharedManager].allCategoryInfoModel.allowVideoListArray) {
                    PublicCategoryElementModel *modellll = [[PublicCategoryElementModel alloc] init];
                    modellll = [tempModel copy];
                    if (self.itemDataArray && self.itemDataArray.count) {
                        for (NSString *str in self.itemDataArray) {
                            if ([str integerValue] == [modellll.id integerValue]) {
                                modellll.isSelect = YES;
                                break;
                            }
                        }
                    }
                    [twoModel.childArray addObject:modellll];
                }
            }
            [dataArray addObject:twoModel];
        }
        
        if (self.type == CompanyPrivacySettingFilterTypeOfPush || self.type == PersonalPrivacySettingFilterTypeOfPush) {
            if ([DataToolsControl sharedManager].allCategoryInfoModel && [DataToolsControl sharedManager].allCategoryInfoModel.jobPushBanListArray.count) {
                PublicCategoryElementModel *threeModel = [[PublicCategoryElementModel alloc] init];
                threeModel.id = @"3";
                threeModel.name = @"禁止推送的人";
                threeModel.detail = @"选择禁止推送的人，可多选";
                threeModel.selectType = SelectTypeOfMultiSelect;
                threeModel.isSelect = ([self.selectType isEqualToString:@"3"]) ? YES : NO;;
                threeModel.childArray = [NSMutableArray arrayWithCapacity:0];
                
                for (PublicCategoryElementModel *tempModel in [DataToolsControl sharedManager].allCategoryInfoModel.jobPushBanListArray) {
                    PublicCategoryElementModel *modellll = [[PublicCategoryElementModel alloc] init];
                    modellll = [tempModel copy];
                    if (self.itemDataArray && self.itemDataArray.count && threeModel.isSelect) {
                        for (NSString *str in self.itemDataArray) {
                            if ([str integerValue] == [modellll.id integerValue] ) {
                                modellll.isSelect = YES;
                                break;
                            }
                        }
                    }
                    [threeModel.childArray addObject:modellll];
                }
                [dataArray addObject:threeModel];
            }
        }
    }
}
- (void)initUI {
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH-50*Scale, HeightOfStatusBar+3*Scale, 50*Scale, 36) imageName:nil target:self action:@selector(okBtnTap) title:@"确定" titleColor:THEMECOLOR_OF_ORANGE];
    okBtn.titleLabel.font = Font(Font_15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:okBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    filterTableview = [UIToolsControl initTableViewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height) target:self style:UITableViewStyleGrouped];
    filterTableview.showsVerticalScrollIndicator = NO;
    filterTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    filterTableview.backgroundColor = [UIColor whiteColor];
    filterTableview.sectionIndexBackgroundColor = [UIColor clearColor];
    filterTableview.rowHeight = 45*Scale;
    [self.view addSubview:filterTableview];
}

- (void)okBtnTap {
    BOOL isSelect = NO;
    for (PublicCategoryElementModel *mmmodel in dataArray) {
        if (mmmodel.isSelect) {
            isSelect = YES;
        }
    }
    if (!isSelect) {
        SVPHUDError(@"您为选择，请选择");
        return;
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    
    NSString *url = CompanyMine_SaveViewPrivacy_URL;
    
    if ((self.type == CompanyPrivacySettingFilterTypeOfName)||(self.type == PersonalPrivacySettingFilterTypeOfName)) {
        NSString *display_name = @"4";
        PublicCategoryElementModel *firstModel = dataArray.firstObject;
        if (firstModel.isSelect) {
            display_name = @"3";
            backString = @"先生/女士";
        }
        
        PublicCategoryElementModel *twoModel = dataArray[1];
        if (twoModel.isSelect) {
            display_name = @"4";
            backString = @"昵称";
        }
        [dic setObject:display_name forKey:@"display_name"];
    } else {
        NSString *allow = @"1";
        NSMutableArray *allow_list = [NSMutableArray arrayWithCapacity:0];
        NSMutableArray *ban_list = [NSMutableArray arrayWithCapacity:0];
        
        PublicCategoryElementModel *firstModel = dataArray.firstObject;
        if (firstModel.isSelect) {
            allow = @"1";
            backString = @"所有人";
        }
        PublicCategoryElementModel *twoModel = dataArray[1];
        if (twoModel.isSelect) {
            allow = @"2";
            backString = @"推送给部分人";
            for (PublicCategoryElementModel *tempModel in twoModel.childArray) {
                if (tempModel.isSelect) {
                    [allow_list addObject:tempModel.id];
                }
            }
        }
        if (self.type == CompanyPrivacySettingFilterTypeOfPush) {
            PublicCategoryElementModel *threeModel = dataArray[2];
            if (threeModel.isSelect) {
                allow = @"3";
                backString = @"禁止推送的人";
                for (PublicCategoryElementModel *tempModel in threeModel.childArray) {
                    if (tempModel.isSelect) {
                        [ban_list addObject:tempModel.id];
                    }
                }
            }
        }
        [dic setObject:allow forKey:@"allow"];
        [dic setObject:allow_list forKey:@"allow_list"];
        [dic setObject:ban_list forKey:@"ban_list"];
    }
    
    if (self.isPerson) { //个人
        switch (self.type) {
            case PersonalPrivacySettingFilterTypeOfLook: {
                url = Setting_saveViewPrivacy_URL;
            } break;
            case PersonalPrivacySettingFilterTypeOfPush: {
                url = Setting_savePushPrivacy_URL;
            } break;
            case PersonalPrivacySettingFilterTypeOfName: {
                url = Setting_saveDisplayName_URL;
            } break;
            default:
                break;
        }
    }else{
        switch (self.type) {
            case CompanyPrivacySettingFilterTypeOfLook: {
                url = CompanyMine_SaveViewPrivacy_URL;
            } break;
            case CompanyPrivacySettingFilterTypeOfPush: {
                url = CompanyMine_SavePushPrivacy_URL;
            } break;
            case CompanyPrivacySettingFilterTypeOfVideo: {
                url = CompanyMine_SaveVideoPrivacy_URL;
            } break;
            case CompanyPrivacySettingFilterTypeOfName: {
                url = CompanyMine_SaveDisplayNamePrivacy_URL;
            } break;
            default:
                break;
        }
    }
    SVPHUDShow;
    __weak typeof(self) weakSelf = self;
    [BaseNetManager requestWithMethod:POST path:url params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(responseObject[@"msg"]);
            if (self.delegate && [self.delegate respondsToSelector:@selector(companyPrivacySettingFilterViewControllerType:info:selectType:selectArray:)]) {
                if (weakSelf.type == CompanyPrivacySettingFilterTypeOfName) {
                    [weakSelf.delegate companyPrivacySettingFilterViewControllerType:weakSelf.type info:self->backString selectType:dic[@"display_name"] selectArray:nil];
                }else{
                  [weakSelf.delegate companyPrivacySettingFilterViewControllerType:weakSelf.type info:self->backString selectType:dic[@"allow"] selectArray:dic[@"allow_list"]];
                }
            }
            [self performSelector:@selector(delayClick) withObject:nil afterDelay:0.5];
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)delayClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    PublicCategoryElementModel *model = dataArray[section];
    return model.childArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PrivacySettingCell *cell = [tableView dequeueReusableCellWithIdentifier:isCellID];
    if (!cell) {
        cell = [[PrivacySettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:isCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    PublicCategoryElementModel *model = dataArray[indexPath.section];
    PublicCategoryElementModel *childmodel = model.childArray[indexPath.row];
    [cell reloadDataModel:childmodel];
    
    return cell;
}
#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PublicCategoryElementModel *model = dataArray[indexPath.section];
    if (model.isSelect) { // 外层勾选
        PublicCategoryElementModel *currentmodel = model.childArray[indexPath.row];
        if (model.selectType == SelectTypeOfTheRadio) { // 单选
            for (PublicCategoryElementModel *childmodel in model.childArray) {
                if (childmodel == currentmodel) {
                    childmodel.isSelect = YES;
                }else{
                    childmodel.isSelect = NO;
                }
            }
        } else { // 多选
            currentmodel.isSelect = !currentmodel.isSelect;
        }
        [filterTableview reloadData];
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    PublicCategoryElementModel *model = dataArray[section];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 65*Scale)];
    view.backgroundColor = COLOR_OF_WHITE;
    view.tag = 500+section;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionClick:)];
    [view addGestureRecognizer:tap];
    
    UIImageView *boximage = [UIToolsControl initImageViewWithFrame:CGRectMake(30*Scale, 22.5*Scale, 20*Scale, 20*Scale) imageName:((model.isSelect)?@"privacysetting_main_s":@"privacysetting_main_n")];
    [view addSubview:boximage];
    
    UILabel *titleLabel = [UIToolsControl initLabelWithFrame:CGRectMake(EndX(boximage)+10*Scale, 10*Scale, WIDTH-100*Scale, 25*Scale) text:model.name textColor:THEMECOLOR_OF_TEXT_333333 font:Font_15];
    [view addSubview:titleLabel];
    
    UILabel *detailLabel = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(titleLabel), EndY(titleLabel), ViewWidth(titleLabel), 18*Scale) text:model.detail textColor:THEMECOLOR_OF_TEXT_9a9a9a font:Font_12];
    [view addSubview:detailLabel];
    
    UIView *line = [UIToolsControl initViewWithFrame:CGRectMake(30*Scale, 64.5*Scale, WIDTH -60*Scale, lineH) backgroundColor:THEMECOLOR_OF_TEXT_cccccc];
    [view addSubview:line];
    
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 65*Scale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001f;
}

#pragma  mark -- Action
- (void)sectionClick:(UITapGestureRecognizer *)tap {
    NSInteger index = tap.view.tag-500;
    PublicCategoryElementModel *checkmodel = dataArray[index];
    if (checkmodel.isSelect == YES) {
        return;
    }
    for (PublicCategoryElementModel *model in dataArray) {
        if (model == checkmodel) {
            model.isSelect = YES;
        } else {
            model.isSelect = NO;
        }
        
        if (model.childArray.count) {
            for (PublicCategoryElementModel *childModel in model.childArray) {
                childModel.isSelect = NO;
            }
        }
    }
    [filterTableview reloadData];
}

@end
